<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <title>Profile Page</title>
        <?php include 'jquery.php'; ?>
        <script type="text/javascript" src="functions.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                check_top();
                $('#label2').delay(1000).fadeOut('slow');
                //$(".float_right").width($(".float_right").width()-$(".float_left").width());

            });
            function success(text) {
                document.getElementById("label2").style.color = "green";
                document.getElementById("label2").innerHTML= text;
            }
           
        </script>
    </head>
    <body>
        <p align="center"><label id="label2"></label></p><br />
        <form name= "form" action="editInfo.php" method = "post">
            <?php
            //Author Nour Ossama
            //starting session
            if (isset($_SESSION['lang']))
                require_once "./language_files/profilePage_" . $_SESSION['lang'] . ".php";
            else
                require_once "./language_files/profilePage_en.php";
            //include fucntions.php
            require_once "functions.php";
            //check if the variable is passed from the "My Profile" button
            if (isset($_GET['property_variable'])) {
                /* this is a member trying to access his own profile
                 *  take id from session and store in variable $member_id
                 */
                if (isset($_GET['type'])) {
                    if ($_GET['type'] == edit) {
                        echo '<script type="text/javascript">
                         success("Changes applied successfully");   
                         </script>';
                    }
                }
                $member_id = $_SESSION['member_id'];
                //calling to execute function if condition was satisfied
                ProfilePage($member_id);
            }
            //if variable was not passed
            else if (!isset($_GET['property_variable'])) {
                /* this is a member trying to access profiles of other members
                 * so take member_id passed from previous page by get and store
                 * in variable $memberID
                 */
                $memberID = intval($_GET['member_id']);
                //calling to execute function if condition was satisfied
                ProfilePage($memberID);
            } else {//if nothing was passed
                echo $lang['message_1'];
            }
            /*
             * function ProfilePage takes member_id and returns a table
             * with all the info about the member
             */

            function ProfilePage($member_id) {
                global $lang;
                /*
                 * query check _member checks if the member entered any info other than the email and password if not,
                 * a message will be displayed otherwise the profile page is viewed
                 */
                $check_member = mysql_query("SELECT *
                                     FROM member
                                     WHERE member_id = '$member_id'") or die(mysql_error());
                while ($row = mysql_fetch_assoc($check_member)) {
                    if ($row['first_name'] == NULL && $row['last_name'] == NULL && $row ['phoneNum'] == NULL
                            && $row['fax'] == NULL && $row['address'] == NULL && $row['affiliation'] == NULL
                            && $row ['fb_page'] == NULL && $row['linked_page'] == NULL && $row['website'] == NULL
                            && $row['research_gate_site'] == NULL && $row['academic_site'] == NULL && $row['mendeley_site'] == NULL) {
                        //display a message to the member
                        echo "<p align='center'>" . $lang['message_2'] . "</p>";
                        // echo "<a href='EditInfo.php?member_id=$member_id'>Edit my profile </a>";
                    } else {

                        if (!isset($_GET['member_id'])) {
                            //displaying a hyperlink at the top right corner that redirects to the EditInfo page
                            print "<fieldset><legend>Options</legend><div class'links'><a href=\"editInfo.php\">" . $lang['message_3'] . "</a> &nbsp;
                            <a href=\"changepass.php\">Change password</a></div></fieldset>";
                        }
                        
                        //using get_first_name function to display first name
                        $first_name = get_first_name($member_id);
                        //using get_last_name function to display last name
                        $last_name = get_last_name($member_id);
                        //displaying first and last names as the page header
                        print "<h1>{$first_name} {$last_name}</h1><br />";
                        //query that retrieves the member's first name
                        $get_first_name = mysql_query("SELECT first_name
                                               FROM member
                                               WHERE member_id = '$member_id'") or die(mysql_error());
                        //open table

                        $image_path = mysql_query("SELECT profilepic_path FROM member WHERE
                            member_id = $member_id") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($image_path)) {
                            $image = $row['profilepic_path'];
                            echo "<table class='no_border'><tr><td width='200' height='290'><div class='float_left' style='border:2px #c3c3c3 outset;'>

                                        <img src='{$image}' alt='profile picture'  width='200' height='290'/></div></td>";
                        }
                        echo "<td><div><table class='profile'>";
                        echo '
               <tr>
                <td><b>' . $lang['f_name'] . '</b></td>';
                        //fetch first name

                        while ($row = mysql_fetch_assoc($get_first_name)) {
                            /*
                             * check if first name is null i.e no first name was entered
                             * then display an empty cell
                             */
                            if ($row['first_name'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $first_name = formatText_safe($row['first_name']);
                                //display the member's first name in a cell
                                echo "<td>{$first_name}</td>
                        </tr>";
                            }
                        }

                        echo '<tr>
                    <td><b>' . $lang['l_name'] . '</b></td>';
                        //query that retrieves the member's last name
                        $get_last_name = mysql_query("SELECT last_name
                                              FROM member
                                              WHERE member_id = '$member_id'") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($get_last_name)) {
                            /*
                             * check if last name is null i.e no last name was entered
                             * then display an empty cell
                             */

                            if ($row['last_name'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $last_name = formatText_safe($row['last_name']);
                                //display the member's last name in a cell
                                echo "<td>{$last_name}</td>
                        </tr>";
                            }
                        }
                        echo "<tr>
                    <td><b>" . $lang['email'] . "</b></td>";
                        //query that retrieves the member's main email
                        $get_main_email = mysql_query("SELECT email
                                               FROM member
                                               WHERE member_id = '$member_id'") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($get_main_email)) {
                            /*
                             * check if email is null i.e no email was entered
                             * then display an empty cell
                             */
                            if ($row['email'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $email = formatText_safe($row['email']);
                                //display the member's main email in a cell
                                echo "<td>{$email}</td>
                        </tr>";
                            }
                        }
                        //query that retireves other emails of the member
                        $get_other_email = mysql_query("SELECT email
                                                FROM emails
                                                WHERE member_id = '$member_id'") or die(mysql_error());
                        //check if the result of the query is empty
                        if (!mysql_num_rows($get_other_email)) {
                            echo "<tr>
                        <td><b>" . $lang['O_email'] . "</b></td>";
                            echo "<td>-</td></tr>";
                            //check if there is other emails in the db, display in table cell
                        } else if ($get_other_email) {
                            while ($row = mysql_fetch_assoc($get_other_email)) {
                                echo "<tr>
                        <td><b>Other Email(s):</b></td>
                        <td>{$row['email']}</td>
                        </tr>";
                            }
                        }
                        echo "<tr>
                    <td><b>" . $lang['phone'] . "</b></td>";
                        //query that retrieves member's phone number
                        $get_phone_num = mysql_query("SELECT phoneNum
                                              FROM member
                                              WHERE member_id = '$member_id'") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($get_phone_num)) {
                            /*
                             * check if phone is null i.e no phone num  was entered
                             * display an empty cell
                             */
                            if ($row['phoneNum'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $phone = formatText_safe($row['phoneNum']);
                                //display the member's phone num in a cell
                                echo "<td>{$phone}</td>";
                            }
                            echo "</tr>";
                        }
                        echo "<tr>
                    <td><b>" . $lang['fax'] . "</b></td>";
                        //query that retrieves member's fax number
                        $get_fax = mysql_query("SELECT fax
                                        FROM member
                                        WHERE member_id = '$member_id'") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($get_fax)) {
                            /*
                             * check if fax is null i.e no phone num  was entered
                             * display an empty cell
                             */
                            if ($row['fax'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $fax = formatText_safe($row['fax']);
                                //display the member's phone num in a cell
                                echo "<td>{$fax}</td>";
                            }
                            echo "</tr>";
                        }
                        echo "<tr>
                    <td><b>" . $lang['address'] . "</b></td>";
                        //query that retrieves member's address
                        $get_address = mysql_query("SELECT address
                                            FROM member
                                            WHERE member_id = '$member_id'") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($get_address)) {
                            /*
                             * check if address is null i.e no address was entered
                             * display an empty cell
                             */
                            if ($row['address'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $address = formatText_safe($row['address']);
                                //display the member's address in a cell
                                echo "<td>{$address}</td>";
                            }
                            echo "</tr>";
                        }
                        echo "<tr>
                    <td><b>" . $lang['affiliation'] . "</b></td>";
                        //query that retrieves member's address
                        $get_affiliation = mysql_query("SELECT affiliation
                                                FROM member
                                                WHERE member_id = '$member_id'") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($get_affiliation)) {
                            if ($row['affiliation'] == NULL) {
                                /*
                                 * check if affiliation is null i.e no affiliation was entered
                                 * display an empty cell
                                 */
                                echo "<td>-</td>";
                            } else {
                                $affiliation = formatText_safe($row['affiliation']);
                                //display the member's affiliation in a cell
                                echo "<td>{$affiliation}</td>";
                            }
                            echo "</tr>";
                        }
                        echo "<tr>
                    <td><b>" . $lang['facebook'] . "</b></td>";
                        //query that retrieves member's facebook page
                        $get_fbpage = mysql_query("SELECT fb_page
                                           FROM member
                                           WHERE member_id = '$member_id'") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($get_fbpage)) {
                            /*
                             * check if fb_page is null i.e no facebook page was entered
                             * display an empty cell
                             */
                            if ($row['fb_page'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $fb = formatText_safe($row['fb_page']);
                                //display the member's facebook page url in a cell
                                echo "<td>{$fb}</td>";
                            }
                            echo "</tr>";
                        }
                        echo "<tr>
                    <td><b>" . $lang['link'] . "</b></td>";

                        //query that retrieves member's LinkedIn page
                        $get_linked_page = mysql_query("SELECT linked_page
                                                FROM member
                                                WHERE member_id = '$member_id'") or die(mysql_error());

                        while ($row = mysql_fetch_assoc($get_linked_page)) {
                            /*
                             * check if linked_page is null i.e no LinkedIn page was entered
                             * display an empty cell
                             */
                            if ($row['linked_page'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $linked_page = formatText_safe($row['linked_page']);
                                //display the member's LinkedIn page url in a cell
                                echo "<td>{$linked_page}</td>";
                            }
                            echo "</tr>";
                        }
                        echo "<tr>
                    <td><b>" . $lang['website'] . "</b></td>";
                        //query that retrieves member's website
                        $get_website = mysql_query("SELECT website
                                            FROM member
                                            WHERE member_id = '$member_id'") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($get_website)) {
                            /*
                             * check if website is null i.e no website was entered
                             * display an empty cell
                             */
                            if ($row['website'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $website = formatText_safe($row['website']);
                                //display the member's website url in a cell
                                echo "<td>{$website}</td>";
                            }
                            echo "</tr>";
                        }
                        echo "<tr>
                    <td><b>" . $lang['research_site'] . "</b></td>";
                        //query that retrieves member's research gate site
                        $get_research_gate_site = mysql_query("SELECT research_gate_site
                                                       FROM member
                                                       WHERE member_id = '$member_id'") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($get_research_gate_site)) {
                            /*
                             * check if research gate site is null i.e no research gate site was entered
                             * display an empty cell
                             */
                            if ($row['research_gate_site'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $research_gate = formatText_safe($row['research_gate_site']);
                                //display the member's research gate site url in a cell
                                echo "<td>{$research_gate}</td>";
                            }
                            echo "</tr>";
                        }
                        echo "<tr>
                    <td><b>" . $lang['academic_site'] . "</b></td>";
                        //query that retrieves member's academic site
                        $get_academic_site = mysql_query("SELECT academic_site
                                                  FROM member
                                                  WHERE member_id = '$member_id'") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($get_academic_site)) {
                            /*
                             * check if academic site is null i.e no academic site was entered
                             * display an empty cell
                             */
                            if ($row['academic_site'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $academic_site = formatText_safe($row['academic_site']);
                                //display the member's academic site url in a cell
                                echo "<td>{$academic_site}</td>";
                            }
                            echo "</tr>";
                        }
                        echo "<tr>
                    <td><b>" . $lang['mendely_site'] . "</b></td>";
                        //query that retrieves member's mendeley site
                        $get_mendeley_site = mysql_query("SELECT mendeley_site
                                                  FROM member
                                                  WHERE member_id = '$member_id'") or die(mysql_error());
                        while ($row = mysql_fetch_assoc($get_mendeley_site)) {
                            /*
                             * check if mendeley site is null i.e no mendeley site was entered
                             * display an empty cell
                             */
                            if ($row['mendeley_site'] == NULL) {
                                echo "<td>-</td>";
                            } else {
                                $mendeley_site = formatText_safe($row['mendeley_site']);
                                //display the member's mendeley site url in a cell
                                echo "<td>{$mendeley_site}</td>";
                            }
                            echo "</tr>";
                        }
                        //close table
                        echo '</table></div></td></tr></table>';
                    }
                }
            }
            ?>
        </form>
    </body>
</html>
